package com.seu.dm.controllers;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.seu.dm.annotations.permissions.BuyerPermission;
import com.seu.dm.dto.CartProductDTO;
import com.seu.dm.dto.UserBaseDTO;
import com.seu.dm.entities.*;
import com.seu.dm.helpers.PageGenerateHelper;
import com.seu.dm.helpers.alipay.constants.AlipayServiceEnvConstants;
import com.seu.dm.helpers.alipay.factory.AlipayAPIClientFactory;
import com.seu.dm.helpers.mail.MailSender;
import com.seu.dm.services.*;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.objects.Global.Infinity;
import static jdk.nashorn.internal.objects.Global.undefined;

/**
 * Created by Greeting on 2017/2/28.
 * 商品相关的控制层逻辑，根据方法使用者的身份添加不同的权限
 * SellerPermission,BuyerPermission,游客默认无权限
 */

@Controller
@RequestMapping(value = "/buyer")
public class BuyerController {
    @Autowired
    private BuyerService buyerService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private OrderProductService orderProductService;

    /**
     * 检查买家用户名是否已存在
     * @param name
     * @return
     */
    @RequestMapping(value="/checkregister/{name}")
    @ResponseBody
    public Boolean checkRegister(@PathVariable String name){
        if(buyerService.findBuyerByName(name)!=null){
            return false;
        }
        return true;
    }

    /**
     * 注册用户
     * @param buyer
     * @param model
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String addBuyer(Buyer buyer, Model model, HttpServletRequest request){

        buyer.setIsActive(false);//置标志位为未激活
        buyer.setIsBanned(false);
        HttpSession httpSession = request.getSession();
        System.out.println("call");

        String name = buyer.getName();
        if(buyerService.findBuyerByName(name)!=null){
            model.addAttribute("message","用户名已存在");
            model.addAttribute("jumpUrl","/register");
            return "common/alert";
        }

        Integer campusId = (Integer)httpSession.getAttribute("campusId");
        System.out.println(campusId);
        buyer.setCampusId(campusId);

        buyerService.addBuyer(buyer);//向数据库中添加买家
        Buyer buyerFromDB = buyerService.findBuyerByName(buyer.getName());


        MailSender.send(buyerFromDB.getId(),buyerFromDB.getEmail());//发送验证激活邮件

        UserBaseDTO userBase = new UserBaseDTO();
        userBase.setRole("buyer");
        userBase.setId(buyerFromDB.getId());
        userBase.setLogin(true);
        userBase.setCampusId(1);
        httpSession.setAttribute("userBase",userBase);
        return "redirect:/";
    }

    /**
     * 登陆时确认是否为一个用户
     * @return
     */
    @RequestMapping(value = "/loginConfirm",method = RequestMethod.POST)
    public String confirmBuyer(Buyer buyer,Model model){
        Integer id = buyer.getId();
        Buyer buyer1 = buyerService.findBuyer(id);
        System.out.println("ok");
        return "index";
    }

    /**
     * 买家登录
     * @param buyer
     * @param request
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String loginBuyer(Buyer buyer,HttpServletRequest request){
//        Integer studentNumber = buyer.getStudentNumber();
//        Buyer buyerFromDB = buyerService.findBuyerByStudentNumber(studentNumber);
        String buyerName = buyer.getName();
        Buyer buyerFromDB = buyerService.findBuyerByName(buyerName);
        if(buyerFromDB == null) return "buyer/register";
        if(buyer.getPassword().equals(buyerFromDB.getPassword())) {
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("user", buyer);
            return "redirect:/";
        }
        return "";
    }

    /**
     * 移除session中的userBase并回到首页
     * @param request
     * @return
     */
    @RequestMapping(value = "/buyerLogout")
    @BuyerPermission
    public String logoutBuyer(HttpServletRequest request){
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null) return "redirect:/";
        httpSession.removeAttribute("userBase");
        return "redirect:/";
    }
    /**
     **买家用户修改信息
     */
    @RequestMapping(value = "/buyerSetting",method = RequestMethod.POST)
    @BuyerPermission
    public String buyerSetInfo(Buyer buyer,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId=userBase.getId();
        Buyer buyerFromDB = buyerService.findBuyer(buyerId);
        buyerFromDB.setPassword(buyer.getPassword());
        buyerFromDB.setEmail(buyer.getEmail());
        buyerFromDB.setMobile(buyer.getMobile());
        buyerService.updateBuyer(buyerFromDB);
        model.addAttribute("message","用户信息修改成功");
        model.addAttribute("jumpUrl","/buyer/buyerSetting");
        return "common/alert";
    }

    /**
     * @param request
     * @param model
     * @return
     * 跳到买家已买到的宝贝
     */
    @RequestMapping(value = "/orders")
    @BuyerPermission
    public String findBuyerOrders(@RequestParam(required = false,defaultValue = "1") Integer pageNum,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderService.findOrdersByBuyerId(buyerId);
        PageInfo pageInfo = new PageInfo(orders);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        model.addAttribute("orders",orders);
        return "buyer/orders";
    }

    /**
     * 查看待发货的宝贝
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/orders/waitdeliver")
    @BuyerPermission
    public String findBuyerOrdersWithStatusWaitDeliver(@RequestParam(required = false,defaultValue = "1") Integer pageNum,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderService.findOrdersByBuyerIdWithStatusWaitDeliver(buyerId);
        PageInfo pageInfo = new PageInfo(orders);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        model.addAttribute("orders",orders);
        return "buyer/ordersWaitdeliver";
    }

    /**
     * 查看已发货的宝贝
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/orders/alreadydeliver")
    @BuyerPermission
    public String findBuyerOrdersWithStatusAlreadyDeliver(@RequestParam(required = false,defaultValue = "1") Integer pageNum,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderService.findOrdersByBuyerIdWithStatusAlreadyDeliver(buyerId);
        PageInfo pageInfo = new PageInfo(orders);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        model.addAttribute("orders",orders);
        return "buyer/ordersAlreadydeliver";
    }

    /**
     * 查看退货中的宝贝
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/orders/onrejection")
    @BuyerPermission
    public String findBuyerOrdersWithStatusOnRejection(@RequestParam(required = false,defaultValue = "1") Integer pageNum,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderService.findOrdersByBuyerIdWithStatusOnRejection(buyerId);
        PageInfo pageInfo = new PageInfo(orders);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        model.addAttribute("orders",orders);
        return "buyer/ordersOnrejection";
    }

    /**
     * 查看已退货的宝贝
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/orders/alreadyrejection")
    @BuyerPermission
    public String findBuyerOrdersWithStatusAlreadyRejection(@RequestParam(required = false,defaultValue = "1") Integer pageNum,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderService.findOrdersByBuyerIdWithStatusAlreadyRejection(buyerId);
        PageInfo pageInfo = new PageInfo(orders);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        model.addAttribute("orders",orders);
        return "buyer/ordersAlreadyrejection";
    }

    /**
     * 查看交易成功的宝贝
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/orders/success")
    @BuyerPermission
    public String findBuyerOrdersWithStatusSuccess(@RequestParam(required = false,defaultValue = "1") Integer pageNum,HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderService.findOrdersByBuyerIdWithStatusSuccess(buyerId);
        PageInfo pageInfo = new PageInfo(orders);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        model.addAttribute("orders",orders);
        return "buyer/ordersSuccess";
    }
    /*
    *跳转到买家登录
     */
    @RequestMapping(value = "/buyer_login")
    public String jumpToBuyerLogin(){
        return "buyer/buyer_login";
    }

    /*
    *跳转到买家注册
     */
    @RequestMapping(value = "/buyer_register")
    public String jumpToBuyerRegister(){
        return "buyer/buyer_register";
    }

    /*
    *跳到买家中心
     */
    @RequestMapping(value = "/center")
    @BuyerPermission
    public String jumpToBuyerCenter(HttpSession httpSession,Model model){
        UserBaseDTO userBase = (UserBaseDTO)httpSession.getAttribute("userBase");
        Buyer buyer = buyerService.findBuyer(userBase.getId());
        model.addAttribute("buyer",buyer);
        return "buyer/buyer_center";
    }

    /**
     * 空购物车提示页面
     *
     */
//    @RequestMapping(value = "/noProductsInCart")
//    public String noProduct(){
//        return "buyer/noProductsInCart";
//    }

    /**
     *生成订单
     *并移除session中的cart回到买家中心
     * @param httpSession
     * @param model
     * @return
     */
    @RequestMapping(value = "/buyatonce")
    public String buyProductAtOnce(HttpSession httpSession,Model model){
        Order order = new Order();
        OrderProduct op =new OrderProduct();

        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        return "";
    }

    /**
     * 买家向购物车中添加商品
     * @param httpSession
     * @param model
     * @return
     */
    @RequestMapping(value = "/order")
    @BuyerPermission
    public String addOrder(HttpSession httpSession,Model model){

        Map<Integer,Integer> cartMap = (Map<Integer,Integer>)httpSession.getAttribute("cart");
        if(httpSession.getAttribute("cart") == null || httpSession.getAttribute("cart") == undefined){
            //如果购物车为空
            model.addAttribute("message","购物车为空不能下单");
            model.addAttribute("jumpUrl","/buyer/center");
            return "common/alert";
        }
        else{


            UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
            Integer buyerId = userBase.getId();
            for(Map.Entry<Integer,Integer> entry : cartMap.entrySet()){
                //遍历购物车
                OrderProduct op = new OrderProduct();
                Order order = new Order();
                Integer productid = (Integer) entry.getKey();
                Integer num = (Integer) entry.getValue();
                Product product=productService.findProduct(productid);
                Seller seller=sellerService.findSeller(product.getSellerId());
                op.setProductId(productid);
                op.setPrice(product.getPrice());
                System.out.println(op.getPrice());
                op.setProductNum(num);
                order.setUserId(buyerId);
                order.setSellerId(seller.getId());
                order.setShopId(seller.getId());
                order.setTotalPrice(product.getPrice().multiply(new BigDecimal(num)));
                order.setOrderProductId(product.getPictureId());
                order.setStatus(1);
                order.setSeller(seller);

                orderService.addOrder(order);
                orderProductService.addOrderProduct(op);
                order.setOrderProductId(op.getId());
                op.setOrderId(order.getId());
                orderService.updateOrder(order);
                orderProductService.updateOrderProduct(op);



                model.addAttribute("order",order);
                model.addAttribute("orderProduct",op);

            }
            httpSession.removeAttribute("cart");
            return "redirect:/buyer/center";
        }

    }

    static int i = 11;
    @RequestMapping(value = "/payorder")
    @BuyerPermission
    public String alipay(HttpServletRequest request, HttpServletResponse response,Model model)throws IOException{

        HttpSession httpSession = request.getSession();

        Map<Integer, Integer> cartMap = (Map<Integer, Integer>) httpSession.getAttribute("cart");

        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        double totalMoney = 0.00;
        for (Map.Entry<Integer, Integer> entry : cartMap.entrySet()) {
            //遍历购物车
            Integer productid = (Integer) entry.getKey();
            Integer num = (Integer) entry.getValue();
            Product product = productService.findProduct(productid);
            Seller seller = sellerService.findSeller(product.getSellerId());
            totalMoney += product.getPrice().doubleValue() * num;
        }
        System.out.println("totalMoney: "+totalMoney);

        AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        System.out.println("called");
        alipayRequest.setReturnUrl("http://139.199.222.147:8080/buyer/order");
        alipayRequest.setNotifyUrl("http://139.199.222.147:8080/buyer/NotifyUrl");

        String orderNum = ("2015032001010112" + (3150+i++)).toString();
        JSONObject json = new JSONObject();
        json.put("out_trade_no",orderNum);
        json.put("total_amount",totalMoney);
        json.put("subject","test");
        json.put("seller_id","2088102169780725");
        json.put("product_code","QUICK_WAP_PAY");

        alipayRequest.setBizContent(json.toString());
        try {
            String form = alipayClient.pageExecute(alipayRequest).getBody();
            response.setContentType("text/html;charset=" + AlipayServiceEnvConstants.CHARSET);
            response.getWriter().write(form);
            response.getWriter().flush();
        }catch (AlipayApiException e){
            e.printStackTrace();
        }
        return "/";
    }

    /**
     *跳到买家购物车
     * @param httpSession
     * @param model
     * @return
     */
    @RequestMapping(value = "/shopping_cart")
    @BuyerPermission
    public String jumpToBuyerShoppingCart(HttpSession httpSession,Model model){
        if(httpSession.getAttribute("cart") == null || httpSession.getAttribute("cart") == undefined){
            model.addAttribute("message","购物车为空！");
            model.addAttribute("jumpUrl","/buyer/center");
            return "common/alert";
        }
        else {
            Map<Integer,Integer> cartMap = (Map<Integer,Integer>)httpSession.getAttribute("cart");
            List<CartProductDTO> cartProducts = new ArrayList<>();
            //遍历cartMap，遍历出来的key是productid，value是这个商品的数量
            //根据id调用Service获得商品的信息，在for里面new一个CartProduct，在CartProduct取好所有数据填好，放进list
            //然后把要的数据放进Model，然后去html里面用th取出来
            for (Map.Entry<Integer, Integer> entry : cartMap.entrySet()) {
                Integer productid = (Integer)  entry.getKey();
                Integer num = (Integer)entry.getValue();
                Product product=productService.findProduct(productid);
                Seller seller= sellerService.findSeller(product.getSellerId());
                CartProductDTO cartProductDTO=new CartProductDTO();
                cartProductDTO.setId(product.getId());
                cartProductDTO.setName(product.getName());
                cartProductDTO.setPictureId(product.getPictureId());
                cartProductDTO.setNum(num);
                cartProductDTO.setPrice(product.getPrice());
                cartProductDTO.setShopName(seller.getShopName());
                cartProducts.add(cartProductDTO);

            }
            model.addAttribute("cartProducts",cartProducts);
            return "buyer/shopping_cart";
        }

    }


    /**
     * 购物车内的商品改变数量
     * @param httpSession
     * @param newNum
     * @param id
     * @return
     */
    @RequestMapping(value = "/shopping_cart_change")
    @BuyerPermission
    @ResponseBody
    public Object changeShoppingCart(@RequestParam Integer id, @RequestParam Integer newNum, HttpSession httpSession){
        Map<Integer,Integer> cartMap = (Map<Integer,Integer>)httpSession.getAttribute("cart");
        cartMap.put(id,newNum);
        return "ok";
    }

    /**
     * 购物车内的商品删除
     * @param id
     * @param httpSession
     * @param model
     * @return
     */
    @RequestMapping(value = "/shopping_cart/remove/{id}")
    @BuyerPermission
    public String removeProductFromCart(@PathVariable Integer id, HttpSession httpSession, Model model){
        Map<Integer,Integer> cartMap = (Map<Integer,Integer>)httpSession.getAttribute("cart");
        cartMap.remove(id);
        httpSession.setAttribute("cartMap",cartMap);
        return "redirect:/buyer/shopping_cart";
    }

    /**
     * 加入购物车
     * @param model
     * @param httpSession
     * @param id
     * @return
     */

    @RequestMapping(value = "/shopping_cart_add")
    @BuyerPermission
    @ResponseBody
    public String addProductFromCart(@RequestParam Integer id, @RequestParam Integer num, HttpSession httpSession, Model model){
        HashMap<Integer,Integer> cartMap = null;
        if(httpSession.getAttribute("cart") == null){
            cartMap = new HashMap<>();
        }else {
            cartMap = (HashMap<Integer, Integer>) httpSession.getAttribute("cart");
        }

        for (Map.Entry<Integer, Integer> entry : cartMap.entrySet()) {
            Integer cartProductId = (Integer)  entry.getKey();
            Integer cartProductNum = (Integer) entry.getValue();
            if (id == cartProductId){
                    entry.setValue(cartProductNum+num);
                    httpSession.setAttribute("cart",cartMap);
                    return "ok";
            }
        }

        cartMap.put(id,num);
        httpSession.setAttribute("cart",cartMap);
        return "ok";
    }


    /**
     * 跳到买家收藏夹
     * @return
     */
    @RequestMapping(value = "/buyer_favorite")
    @BuyerPermission
    public String jumpToBuyerFavorite(){
        return "buyer/buyer_favorite";
    }


    /**
     * 跳到买家已买到的宝贝
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/bought_products")
    @BuyerPermission
    public String jumpToBoughtProducts(HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO buyer = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = buyer.getId();
        List<Order> orders = orderService.findOrdersByBuyerId(buyerId);
        model.addAttribute("orders",orders);
        return "buyer/bought_products";
    }

    /**
     * 跳到买家已买过得店铺
     * @return
     */
    @RequestMapping(value = "/bought_shops")
    @BuyerPermission
    public String jumpToBoughtShops(){
        return "buyer/bought_shops";
    }

    /**
     * 跳到买家评价管理页面
     * @return
     */
    @RequestMapping(value = "/comment_management")
    @BuyerPermission
    public String jumpToCommentManagement(){
        return "buyer/comment_management";
    }

    /**
     * 跳到买家我的足迹
     * @return
     */
    @RequestMapping(value = "/foot_print")
    @BuyerPermission
    public String jumpToFootPrint(){
        return "buyer/foot_print";
    }

    /**
     * 跳到买家我的设置
     * @return
     */

    @RequestMapping(value = "/buyer_setting")
    @BuyerPermission
    public String jumpToBuyerSetting(HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBase = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBase.getId();
        Buyer buyer = buyerService.findBuyer(buyerId);
        model.addAttribute("buyer",buyer);
        return "buyer/buyer_setting";
    }


}
